﻿using BlueportCoffee.IceModel;
using BlueportCoffee.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace BlueportCoffee.LabelingModle
{
    public class LabelMsg
    {
        private static LabelMsg instance;
        private LabelMsg() { }

        public static LabelMsg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LabelMsg();
                }
                return instance;
            }
        }
        public byte[] _usbport = new byte[1024];
        public byte[] _prnport = new byte[1024];
        public string usbname = "";
        public long handle;
        public uint EnumUSB()
        {
            uint usbsun = KLabelLib.EnumPrinterPorts(KLabelLib.USB_PORT, _usbport, _usbport.Length);
            String usbs = System.Text.Encoding.Default.GetString(_usbport);
            String[] usbss = usbs.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
            //uint usbsun = KLabelLib.EnumPrinterPorts(KLabelLib.USB_PORT, _usbport, _usbport.Length);
            //String usbs = System.Text.Encoding.Default.GetString(_usbport);
            //String[] usbss = usbs.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
            if (usbss.Count() > 0)
                usbname = usbss[0];
            return usbsun;
        }
        public bool GetHandler()
        {
            try
            {
                if (usbname.Length <= 0)
                {
                    MessageBox.Show("USB name is empty.");
                    return false;
                }
                handle = KLabelLib.GetPortHandler(KLabelLib.USB_PORT, usbname);
                //handle = (int)KLabelLib.GetPortHandler(KLabelLib.USB_PORT, usbname);
                if (0 == handle)
                {
                    MessageBox.Show("Get USB handler Failed");
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public void USBShow(string labels)
        {
            try
            {
                var msgs = labels.Split('|');
                if (msgs.Count() >= 11)
                {
                    if (false == GetHandler())
                    {
                        return;
                    }
                    if (0 == KLabelLib.OpenPort(handle))
                    {
                        KLabelLib.RelasePortHandler(handle);
                        return;
                    }
                    bool bIsNeedCut = true;
                    KLabelLib.SetCommandType(handle, KLabelLib.CMD_T_TSPL);
                    KLabelLib.TSPL_SetLabelSize(handle, int.Parse("20"), int.Parse("50"));
                    KLabelLib.TSPL_SetLabelVertDistance(handle, 0, 0); //3mm   0mm
                    KLabelLib.TSPL_SetLabelShift(handle, 0); //0 dots
                    KLabelLib.TSPL_SetReference(handle, 0, 0);
                    KLabelLib.TSPL_SetClearGraphCache(handle, 1); //Need to clear graph cache
                    KLabelLib.TSPL_SetPrintSpeed(handle, 4);
                    KLabelLib.TSPL_SetPrintDensity(handle, 7);
                    KLabelLib.TSPL_SetDirection(handle, 0);
                    //  string labels = "Z100|黄皮花魁轻咖|3分糖|少冰|中杯|2025-07-30 13:46:22|打包带走|千里千寻总店|1/5|4001001111|2小时饮用更好喝哦";
                    //Test print label 1
                    #region 40*60
                    //KLabelLib.TSPL_SetPrintText(handle, 80, 300, "1", 270, 3, 3, msgs[0]);
                    //KLabelLib.TSPL_SetPrintText(handle, 80, 80, "0", 270, 1, 1, msgs[8]);
                    //KLabelLib.TSPL_SetPrintText(handle, 120, 110, "0", 270, 1, 1, msgs[6]);
                    //KLabelLib.TSPL_SetPrintText(handle, 160, 300, "1", 270, 1, 2, msgs[1]);
                    //KLabelLib.TSPL_SetPrintText(handle, 220, 300, "1", 270, 1, 1, msgs[4]);
                    //KLabelLib.TSPL_SetPrintText(handle, 220, 240, "1", 270, 1, 1, msgs[3]);
                    //KLabelLib.TSPL_SetPrintText(handle, 220, 150, "0", 270, 1, 1, msgs[2]);
                    //KLabelLib.TSPL_SetPrintText(handle, 285, 300, "0", 270, 1, 1, "招商热线");
                    //KLabelLib.TSPL_SetPrintText(handle, 315, 300, "0", 270, 1, 1, msgs[9]);
                    //KLabelLib.TSPL_SetPrintText(handle, 345, 300, "0", 270, 1, 1, msgs[7]);
                    //KLabelLib.TSPL_SetPrintText(handle, 375, 300, "0", 270, 1, 1, msgs[5]);
                    //KLabelLib.TSPL_SetPrintText(handle, 405, 280, "0", 270, 1, 2, msgs[10]);
                    //KLabelLib.TSPL_SetPrintBitmap(handle, 3, 3, System.Text.Encoding.Unicode.GetBytes("D:\\123.bmp"), 0);
                    //KLabelLib.TSPL_SetPrintBitmap(handle, 285, 30, System.Text.Encoding.Unicode.GetBytes("D:\\111.bmp"), 0);
                    #endregion
                    #region 20*50
                    KLabelLib.TSPL_SetPrintText(handle, 13, 365, "0", 270, 1, 2, msgs[6]);
                    KLabelLib.TSPL_SetPrintText(handle, 23, 300, "1", 270, 2, 2, msgs[0]);
                    KLabelLib.TSPL_SetPrintText(handle, 23, 70, "1", 270, 1, 2, msgs[8]);
                    KLabelLib.TSPL_SetPrintText(handle, 60, 365, "0", 270, 1, 1, "----------------------------");
                    KLabelLib.TSPL_SetPrintText(handle, 88, 365, "0", 270, 1, 1, msgs[1]);
                    KLabelLib.TSPL_SetPrintText(handle, 121, 365, "0", 270, 1, 1, msgs[4]);
                    KLabelLib.TSPL_SetPrintText(handle, 121, 300, "0", 270, 1, 1, msgs[3]);
                    KLabelLib.TSPL_SetPrintText(handle, 121, 220, "0", 270, 1, 1, msgs[2]);
                    KLabelLib.TSPL_SetPrintBitmap(handle, 85, 30, System.Text.Encoding.Unicode.GetBytes("D:\\114.bmp"), 0);
                    #endregion
                    KLabelLib.TSPL_SetPrintCount(handle, 1);
                    if (bIsNeedCut)
                    {
                        KLabelLib.TSPL_SetCut(handle); // 'CUT' command
                    }
                    if (false == KLabelLib.SendPrintCmd(handle))
                    {
                        //Close port and release
                        KLabelLib.ClosePrinterPort(handle);
                        KLabelLib.RelasePortHandler(handle);
                        MessageBox.Show("Send print label1 command Failed");
                        return;
                    }
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
